package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    //TODO 完善条件查询带分页
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list",list);
    }
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        String id = courseService.saveCourseInfo(courseInfoVo);
        return  R.ok().data("courseId",id);
    }

    //根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "分页teacher列表")
    @GetMapping("{page}/{limit}")
    public R pageList(

            @PathVariable Long page,

            @PathVariable Long limit){
        Page<EduCourse> pageParam = new Page<>(page, limit);

        courseService.page(pageParam,null);
        try {
            int a = 10/0;
        }catch (Exception e){
            throw new GuliException(20001,"执行了自定义异常.....");
        }
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();

        return R.ok().data("total",total).data("rows",records);
    }

    @ApiOperation(value = "分页课程列表")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageTeacherCondition(

            @PathVariable Long current,

            @PathVariable Long limit,
//            @ApiParam(name = "teacherQuery", value = "查询对象", required = false)
            @RequestBody(required = false)
                    CourseInfoVo courseQuery){


        Page<EduCourse> pageCourse = new Page<>(current, limit);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();

        String title = courseQuery.getTitle();
        Integer lessonNum = courseQuery.getLessonNum();
        BigDecimal price = courseQuery.getPrice();
        String description = courseQuery.getDescription();

        if (!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(lessonNum)){
            wrapper.eq("lessonNum",lessonNum);
        }
        if (!StringUtils.isEmpty(price)){
            wrapper.ge("price",price);
        }
        if (!StringUtils.isEmpty(description)){
            wrapper.le("description",description);
        }


        wrapper.orderByDesc("price");
        courseService.page(pageCourse,wrapper);

        long total = pageCourse.getTotal();
        List<EduCourse> records = pageCourse.getRecords();
//        List<EduCourse> list = courseService.list(null);
        return R.ok().data("total",total).data("rows",records);

    }

    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
//        boolean flag =
                courseService.removeCourse(courseId);
//        if (flag){
//            return R.ok();
//        }else {
//
//            return R.error();
//        }
        return R.ok();
    }

}

